// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   Main.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package uoc.libwave;

// <editor-fold defaultstate="collapsed" desc="imports">
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// </editor-fold>

/**
 * Class for storing the chunks of an audio WAVE file, and for reading and
 * writing its content by using Data I/O Streams.
 *
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 1.0
 * @since 2013-08-04
 */
public final class WaveFile extends LittleEndianBinaryFileOps {

    /**
     * Microsoft Pulse Code Modulation (PCM) format.
     */
    public static final short WAVE_FORMAT_PCM = 0x0001;
    /**
     * IEEE float
     */
    public static final short WAVE_FORMAT_IEEE_FLOAT = 0x0003;
    /**
     * 8-bit ITU-T G.711 A-law
     */
    public static final short WAVE_FORMAT_ALAW = 0x0006;
    /**
     * 8-bit ITU-T G.711 U-law
     */
    public static final short WAVE_FORMAT_MULAW = 0x0007;
    /**
     * IBM mu-law format
     */
    public static final short IBM_FORMAT_MULAW = 0x0101;
    /**
     * IBM a-law format
     */
    public static final short IBM_FORMAT_ALAW = 0x0102;
    /**
     * IBM AVC Adaptive Differential Pulse Code Modulation format
     */
    public static final short IBM_FORMAT_ADPCM = 0x0103;
    /**
     * Determined by SubFormat
     */
    public static final short WAVE_FORMAT_EXTENSIBLE = (short) 0xFFFE;
    /**
     * Number of bits of each A-law sample
     */
    protected static final short ALAW_BITS_PER_SAMPLE = 8;
    /**
     * Number of bits of each U-law sample
     */
    protected static final short ULAW_BITS_PER_SAMPLE = 8;
    /**
     * The file path to an audio WAVE file.
     */
    private String strSrcFilePath;
    /**
     *
     */
    public WaveFormat waveData;

    /**
     *
     * @since 2013-08-04
     */
    public WaveFile() {
    }

    /**
     * Reads the ID of a generic chunk from a DataInputStream.
     *
     * @param dis DataInputStream that points correctly to the underlying input
     * stream for reading data.
     * @throws IOException if an input error occurs.
     * @return
     *
     * @since 2013-08-04
     */
    public String readCkID(DataInputStream dis) throws IOException {
        char[] ID = new char[4];
        for (int i = 0; i < 4; i++) {
            ID[i] = readAsciiCharLE(dis);
        }
        return String.valueOf(ID);
    }

    /**
     * Reads the size of a generic chunk from a DataInputStream.
     *
     * @param dis DataInputStream that points correctly to the underlying input
     * stream for reading data.
     * @throws IOException if an input error occurs.
     *
     * @since 2013-08-04
     */
    public int readCkSize(DataInputStream dis) throws IOException {
        return readLongLE(dis);
    }

    /**
     *
     * @param dis DataInputStream that points correctly to the underlying input
     * stream for reading data.
     * @return
     *
     * @since 2013-08-04
     */
    public String readRiffType(DataInputStream dis) throws IOException {
        char[] aux = new char[4];
        for (int i = 0; i < 4; i++) {
            aux[i] = readAsciiCharLE(dis);
        }
        return String.valueOf(aux);
    }

    /**
     * Reads the WAVE format tag from a DataInputStream.
     *
     * @param dis DataInputStream that points correctly to the underlying input
     * stream for reading data.
     * @throws IOException if an input error occurs.
     * @return
     *
     * @since 2013-08-04
     */
    public short readWFormatTag(DataInputStream dis) throws IOException {
        return readShortLE(dis);
    }

    /**
     * Reads the number of channels from a DataInputStream.
     *
     * @param dis DataInputStream that points correctly to the underlying input
     * stream for reading data.
     * @throws IOException if an input error occurs.
     * @return
     *
     * @since 2013-08-04
     */
    public short readWChannels(DataInputStream dis) throws IOException {
        return readShortLE(dis);
    }

    /**
     * Reads the sampling rate from a DataInputStream.
     *
     * @param dis DataInputStream that points correctly to the underlying input
     * stream for reading data.
     * @throws IOException if an input error occurs.
     * @return
     *
     * @since 2013-08-04
     */
    public int readDWSamplesPerSec(DataInputStream dis) throws IOException {
        return readLongLE(dis);
    }

    /**
     * Reads the average number of bytes per second from a DataInputStream.
     *
     * @param dis DataInputStream that points correctly to the underlying input
     * stream for reading data.
     * @throws IOException if an input error occurs.
     * @return
     *
     * @since 2013-08-04
     */
    public int readDWAvgBytesPerSec(DataInputStream dis) throws IOException {
        return readLongLE(dis);
    }

    /**
     * Reads the block alignment (in bytes) from a DataInputStream.
     *
     * @param dis DataInputStream that points correctly to the underlying input
     * stream for reading data.
     * @return
     *
     * @since 2013-08-04
     */
    public short readWBlockAlign(DataInputStream dis) throws IOException {
        return readShortLE(dis);
    }

    /**
     * Reads the bits per sample (in bytes) from a DataInputStream.
     *
     * @param dis DataInputStream that points correctly to the underlying input
     * stream for reading data.
     * @return
     *
     * @since 2013-08-04
     */
    public short readWBitsPerSample(DataInputStream dis) throws IOException {
        return readShortLE(dis);
    }

    /**
     * 
     * @param dis
     * @return
     * 
     * @since 2013-08-30
     */
    public short readCbSize(DataInputStream dis) throws IOException {
        return readShortLE(dis);
    }

    /**
     * 
     * @param dis
     * @return
     * @throws IOException
     * 
     * @since 2013-08-30
     */
    public int readDwSampleLength(DataInputStream dis) throws IOException {
        return readLongLE(dis);
    }

}
